package com.littleHan.brushQuestions.interceptor;

import com.littleHan.brushQuestions.web.start.service.PermissionService;
import com.littleHan.brushQuestions.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyInterceptor implements HandlerInterceptor {

    @Autowired
    private PermissionService permissionService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /*return HandlerInterceptor.super.preHandle(request, response, handler);*/
        //request.getHeader();
        String uri=request.getRequestURI();
        System.out.println(uri);
        System.out.println("拦截器执行");
        String jwt = request.getHeader("Authorization");
        System.out.println("请求头："+jwt);
        Claims claims=null;
        if(jwt!=null && !"null".equals(jwt) && !"".equals(jwt)){
            try{
                //解密去验证jwt
                claims=JwtUtil.parseJWT(jwt);
            }catch (RuntimeException e){
                //抛异常说明jwt不合法，疑似被更改
                System.out.println("被拦截");
                //被拦截打回403状态码告诉前端需要登录
                response.setStatus(403);
                return false;
            }
        }
        if(claims==null){
            System.out.println("token过期或者未登录被拦截");
            //被拦截打回403状态码告诉前端用户未登录需要登录
            response.setStatus(403);
            return false;
        }
        else{
            //解密后获取token里的用户id
            System.out.println("判断是否有权访问");
            String userId=claims.getId();
            if(permissionService==null){
                BeanFactory factory=WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
                permissionService=(PermissionService)factory.getBean("permissionServiceImpl");
            }
            //调用业务层进行判断根据用户id判断当前用户请求的路径是否拥有权限。有权限返回true无权限返回false
            boolean b=permissionService.getPermissionListAndJudge(userId,request.getRequestURI());
            boolean violation=permissionService.isAccountBan(userId);
            if(b==true){ //判断可以简写，但是为了后期遇到麻烦好更改就暂时不简写
                if(violation==true){
                    return true;
                }
                else{
                    //封禁就打回406状态码进行处理
                    response.setStatus(406);
                    return false;
                }
            }
            else{
                //执行了else说明该用户无权访问打回405状态码，前端对405状态码进行处理
                response.setStatus(405);
                return false;
            }
            /*System.out.println("路径："+request.getRequestURI());
            System.out.println("输出"+claims.getId());
            System.out.println("Issuer: " + claims.getIssuer());
            System.out.println("Subject: " + claims.getSubject());*/
        }
    }

}
